<div class="syntax"><pre><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Context managers provide a much nicer way to</span>
<span class="sd">handle book keeping around your file objects.</span>

<span class="sd">For example context managers will automatically</span>
<span class="sd">close the file when the block ends.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
    <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">namelist</span><span class="p">:</span>
        <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="se">\n</span><span class="s">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">name</span><span class="p">,))</span>
</pre></div>
